<template>
  <div class="mod-config">
<!--    类型树-->
    <div class="type-Tree">
      <type-tree></type-tree>
    </div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
<!--      <el-form-item>-->
<!--        <el-button plain @click="detail(1)">新增</el-button>-->
<!--      </el-form-item>-->
    </el-form>
    <el-table
      :data="dataList"
      row-key="id"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column align="center" type="index" header-align="center" width="80px" label="序号">
      </el-table-column>
      <el-table-column prop="description" header-align="center" align="center" label="类型"></el-table-column>
      <el-table-column prop="type" label="编码" header-align="center" align="center"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <!--          <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <el-button type="text" plain @click="detail(2,scope.row.id)">修改</el-button>
          <el-button type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer title="增加目录" :visible.sync="drawer" :wrapperClosable="false">
      <el-form :model="dataForm" style="margin:  0  20px">
        <el-form-item label="选择分类">
          <el-select
            filterable
            v-model="dataForm.type"
            placeholder="模块名称"
            style="width:100%"
          >
            <el-option v-for="item in dataList" :key="item.type" :label="item.description" :value="item.type"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字典名称">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="dataForm.description "></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="dataForm.sort"></el-input>
        </el-form-item>
        <el-form-item>
          <template>
            <el-button @click="drawer = false">取消</el-button>
            <el-button @click="confirm">确定</el-button>
          </template>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 弹窗, 新增 / 修改 -->
  </div>
</template>

<script>
  import TypeTree from './type-tree'
  export default {
    components: {TypeTree},
    data() {
      return {
        dialogFormVisible: false,
        dialogFormAddVisible: 1,
        drawer: false,
        learnCate: false,
        current: {},
        dataForm: {
          type: '',
          name: '',
          description: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    mounted() {
      this.getDataList()
    },
    methods: {
      moduleChange(val) {
        console.log(val)
      },
      checkSort() {
        this.learnCate = true
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs.learnCate.init()
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done()
          })
          .catch(_ => {
          })
      },
      detail(type, id) {
        if (type === 1) {
          this.dataForm = {
            lable: '',
            type: '',
            sort: 1
          }
        } else {
          this.$http({
            url: this.$http.adornUrl(`/active/dict/info/${id}`),
            method: 'post'
          }).then(res => {
            if (res && res.code === 200) {
              this.dataForm = res.data
              this.dataForm.cateName = res.data.parentName
              this.dataForm.id = res.data.id
            } else {
              this.dataForm = {}
            }
            this.dataListLoading = false
          })
        }
        this.dialogFormAddVisible = type
        this.drawer = true
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/dict/certList'),
          method: 'post',
          data: {
            page: this.pageIndex,
            limit: this.pageSize
            // 'key': this.dataForm.key
          }
        }).then(res => {
          if (res && res.code === 200) {
            this.dataList = res.data
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      // 分类
      setCurrent(data) {
        console.log(data)
        this.dataForm.cateId = data.id
        this.dataForm.cateName = data.name
        // this.dataForm.parentId = data.parentId
        // if (data.id) this.current = data
      },
      // async confirm() {
      //   console.log('訪問嗎')
      //   let res = await this.$http({
      //     url: this.$http.adornUrl('usercenter-admin/dict/save',
      //       {
      //         label: this.dataForm.name,
      //         sort: this.dataForm.sort,
      //         type: this.dataForm.type
      //       }),
      //     method: 'post'
      //   })
      //   console.log(res)
      //   // this.moduleList = res.data
      // },
      confirm() {
        this.$http({
          url: '/usercenter-admin/dict/save',
          method: 'post',
          data: {
            label: this.dataForm.name,
            sort: this.dataForm.sort,
            type: this.dataForm.type,
            description: this.dataForm.description
          }
        }).then(res => {
          // if (res && res.code === 200) {
          //   this.dataList[0].children = res.data
          //   if (this.arr.length) {
          //     this.$nextTick(_ => this.$refs.tree.setCheckedKeys(this.arr))
          //   }
          // } else {
          //   this.dataList = []
          // }
          // this.dataListLoading = false
        })
      },
      // confirm() {
      //   this.$http({
      //     url: this.$http.adornUrl(
      //       `usercenter-admin/dict/${
      //         this.dialogFormAddVisible === 1 ? 'save' : 'update'
      //       }`
      //     ),
      //     method: 'post',
      //     data: {
      //       id: this.dataForm.id,
      //       label: this.dataForm.name,
      //       sort: this.dataForm.sort,
      //       type: this.dataForm.type
      //     }
      //   })
      //     .then(res => {
      //       if (res && res.code === 200) {
      //         this.$message({
      //           message: '操作成功',
      //           type: 'success',
      //           duration: 1500
      //         })
      //         this.getDataList()
      //         this.dialogFormAddVisible = 0
      //         this.drawer = false
      //       } else {
      //         this.$message.warning(res.msg)
      //       }
      //       this.dataListLoading = false
      //     })
      //     .catch(_ => {
      //       this.dataListLoading = false
      //     })
      // },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        // var ids = id ? [id] : this.dataListSelections.map(item => {
        //   return item.id
        // })
        // this.fullscreenLoading = true
        this.$confirm(`确定要进行删除操作吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/active/dict/delete'),
            method: 'post',
            data: {id: id}
          }).then(res => {
            if (res && res.code === 200) {
              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              })
              this.getDataList()
            } else {
              this.$notify.error({
                title: '提示',
                message: res.msg
              })
            }
          })
        })
      }
    }
  }
</script>

<style scoped>
  .mod-config{
    display: flex;
    height: 100%;
    width: auto;
    overflow: auto;
    border: 2px solid #f5f5f5;
    border-radius: 2px;
  }
  .type-Tree {
    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/

    ::-webkit-scrollbar {
      width: 2px;
      height: 2px;
      background-color: #f5f5f5;
    }

    /*定义滚动条轨道 内阴影+圆角*/

    ::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      border-radius: 10px;
      background-color: #f5f5f5;
    }

    /*定义滑块 内阴影+圆角*/

    ::-webkit-scrollbar-thumb {
      border-radius: 10px;
      box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      background-color: #c8c8c8;
    }
  }
</style>
